<?php

require_once("Model/Topic.php");

class TopicList
{

		//Privata strängar
		private $db = NULL;
		private $topicTable = "topic";
		private $userTable = "user";
        
		//Konstruktor för databasinstans
        public function __construct(Database $db) 
        {
                $this->db =$db;
				//TODO: Slopa konstruktorn?
        }		
		
		public function getTopic($topicId)
		{
			$sql = "SELECT t.topicId, t.header, u.name, t.date, t.content
					FROM topic AS t
					INNER JOIN user AS u
					ON t.userId = u.userId 
                    WHERE t.topicId =?";
					
			$stmt = $this->db->Prepare($sql);		
			
			$stmt->bind_param("i", $topicId);
			
			$select = $this->db->Select($stmt);
			
			if($select->bind_result($topicId, $header, $userName, $date, $content) == FALSE)
			{
				throw new Exception($this->mysqli->error);
			}	
			
			$ret = NULL;
			
			if($select->fetch())
			{
				$ret = new Topic($topicId, $header, $userName, $date, $content);
			}
			
			$select->close();
			
			return $ret;			
						
		}
				
		public function getAllTopics()
		{
			$sql = "SELECT t.topicId, t.header, u.name, t.date, t.content
					FROM $this->topicTable AS t
					INNER JOIN $this->userTable AS u
					ON t.userId = u.userId 
					ORDER BY t.date DESC";
				
			$stmt = $this->db->Prepare($sql);
			
			$select = $this->db->Select($stmt);
			
			if($select->bind_result($topicId, $header, $userName, $date, $content) == FALSE)
			{
				throw new Exception($this->mysqli->error);
			}	
						
			$ret = new TopicArray();
			
			while ($select->fetch())
			{
				$topic = new Topic($topicId, $header, $userName, $date, $content);
				$ret->add($topic);
			}
			
			$select->close();
			
			return $ret;
						
		}
		
		public static function test(Database $db)
		{
			//TODO: Kika närmre på detta du benke	
			$tl = new TopicList($db);
			
			$allTopics = $tl->getAllTopics($db);
			
			if(get_class($allTopics) != "TopicArray")
			{
				echo "getAllTopics returned" . get_class($allTopics);
				return FALSE;
			}
			
			foreach($allTopics->get() as $topic)
			{
				$actual = $tl->getTopic($topic->getId());
				
				if($actual->getId() != $topic->getId())
				{
					echo "getTopic error 1: Return value from getAllTopics and getTopic differs";
					return FALSE;
				}

				if($actual->getHeader() != $topic->getHeader())
				{
					echo "getTopic error 2: Return value from getAllTopics and getTopic differs";
					return FALSE;
				}

				if($actual->getUserName() != $topic->getUserName())
				{
					echo "getTopic error 3: Return value from getAllTopics and getTopic differs";
					return FALSE;
				}
				
				if($actual->getDate() != $topic->getDate())
				{
					echo "getTopic error 4: Return value from getAllTopics and getTopic differs";
					return FALSE;
				}
				
				if($actual->getContent() != $topic->getContent())
				{
					echo "getTopic error 5: Return value from getAllTopics and getTopic differs";
					return FALSE;
				}																
			}
			
			return TRUE;
			
			
			
			
		}
}


?>